/*
 * Command.h
 *
 *  Created on: 2014. 6. 25.
 *      Author: jjaehuny
 */

#ifndef FRAMEWORK_DATABASE_COMMAND_H_
#define FRAMEWORK_DATABASE_COMMAND_H_

#include <sstream>
#include <vector>
#include <chrono>
#include <memory>
#include <functional>


#include <cppconn/resultset.h>

#include "../Memory/Base_Pool.h"
#include "../Macro.h"

namespace Framework { namespace Database {

/// command class instance return func
class Command;
std::shared_ptr<::Framework::Database::Command> create_command_multithread();
std::shared_ptr<::Framework::Database::Command> create_command_singlethread();

} /*Database*/ } /*Framework*/


namespace Framework { namespace Database {

class Command : public std::enable_shared_from_this<Command>
{
public:
	typedef std::function<void(std::shared_ptr<Command>)> OBSERVER;

public:
	Command();
	virtual ~Command();

	Command(const Command & other) = delete;
	Command & operator= (const Command & other) = delete;

	void Initialize();
	void Finalize();

	std::shared_ptr<Command> CAPTURE();

	// variaric template
	template<class... ARGS>
	void MakeQuery(ARGS &&... args) {
		std::stringstream stream;
		concat(stream, args...);

		query_ = stream.str();
	}

private:
	template<class T, class... ARGS>
	void concat(std::stringstream & stream, const T & t, ARGS &&... args)
	{
		stream << t;
		concat(stream, args...);
	}

	template<class T>
	void concat(std::stringstream & stream, const T & t)
	{
		stream << t;
	}

private:
	GETSETVAR(bool, isSuccess_);
	GETSETVAR_PTR(sql::ResultSet, resultSet_);
	GETVAR(std::string, query_);
	GETSETVAR(OBSERVER, OnResponse_);
	GETSETVAR(int, query_type_);
};

} /*Database*/ } /*Framework*/

#endif /* FRAMEWORK_DATABASE_COMMAND_H_ */
